const Ws = require('ws');
const CIM_HOST = 'localhost';
const CIM_PORT = 34567;
;((Ws) => {
  const server = new Ws.Server({port: 8080});

  const init = () => {
    bindEvent();
  }

  function bindEvent () {
    server.on('open', handleOpen);
    server.on('close', handleClose);
    server.on('error', handleError);
    server.on('connection', handleConnection);
  }
  function handleOpen () {
    console.log('open')
  }

  function handleClose() {
    console.log('close')
  }

  function handleError() {
    console.log('连接失败')
  }

  function handleConnection (ws) {
    console.log('connection');
    ws.on('message', handleMessage);
  }

  function handleMessage (msg) {
    server.clients.forEach(item => {
      item.send(msg.toString())
    });
  }
  init()
})(Ws)